package com.info33.platform.business.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.business.app.dto.AppTenantUserDTO;
import com.info33.platform.business.app.entity.AppTenantUser;
import com.info33.platform.business.app.service.AppTenantUserService;
import com.info33.platform.business.app.vo.AppTenantUserVO;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.RedisGeo;
import com.info33.platform.common.validated.UpdateValidated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppTenantUser")
@Api(tags = "app - 租户 - 用户")
public class AppTenantUserController extends BaseController<AppTenantUserService, AppTenantUser> {

    private final AppTenantUserService appTenantUserService;
    @Autowired
    private RedisGeo redisGeo;

    @Autowired
    public AppTenantUserController(AppTenantUserService appTenantUserService) {
        super(appTenantUserService);
        this.appTenantUserService = appTenantUserService;
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * app
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping(value = "/app/tenants")
    @ApiOperation(value = "app - 获取用户所有租户")
    public Result<List<AppTenantUserVO>> appTenants() {
        return Result.ok(appTenantUserService.appTenants(getCurrentUser()));
    }

    @PatchMapping(value = "/app/switch/{id}")
    @ApiOperation(value = "app - 切换租户", notes = "app - 获取用户所有租户 id")
    public Result<Object> pcSwitch(
            @PathVariable(value = "id") Long id) {
        return appTenantUserService.pcSwitch(id, getCurrentUser());
    }

    @PutMapping(value = "/app/change/{atuId}")
    @ApiOperation(value = "app - 登录到切换的租户")
    public Result<Object> appChange(
            @PathVariable(value = "atuId") Long atuId) {
        return appTenantUserService.appChange(atuId, getCurrentUser());
    }

    @PostMapping(value = "/app/online/location")
    @ApiOperation(value = "app - 在线用户地理位置信息", notes = "位置信息上传用")
    public void appOnlineLocation(
            @RequestBody Point point) {
        redisGeo.add(point.getX(), point.getY(), getTenantUserId() + "");
    }

    @PostMapping("/pc/deriveUserList")
    @ApiOperation(value = "pc - Excel导出")
    public Result<Object> deriveUserList(@RequestBody AppTenantUserDTO dto, HttpServletResponse response) {
        if (!getCurrentUser().getSysTenantUserVO().getUserType().name().equalsIgnoreCase("ADMIN")) {
            return Result.error("仅限管理员可导出数据!");
        }
        return appTenantUserService.deriveUserList(dto,response);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * pc
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 人员列表")
    public Result<IPage<AppTenantUserVO>> pcPage(
            @RequestBody AppTenantUserDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(appTenantUserService.pcPage(new Page<>(pageNum, pageSize), dto, getCurrentUser()));
    }

    @GetMapping(value = "/pc/detail/{uid}")
    @ApiOperation(value = "pc - 人员详情", notes = "pc - 人员列表：id")
    public Result<AppTenantUserVO> pcDetail(
            @PathVariable(value = "uid") Long uid) {
        return Result.ok(appTenantUserService.pcDetail(uid));
    }

    @PutMapping(value = "/pc/edit")
    @ApiOperation(value = "pc - 编辑", notes = "用户基础信息 + 角色绑定关系")
    public Result<Object> pcEdit(
            @Validated(value = {AppTenantUserDTO.PcEdit.class, UpdateValidated.class}) @RequestBody AppTenantUserDTO dto) {
        return appTenantUserService.pcEdit(dto);
    }

}